<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="/js/jquery-3.4.1.min.js"></script>
</head>
<body>
<h3>部门信息管理</h3>
<table border="1" width="80%">
    <thead>
    <tr>
        <td>姓名</td>
        <td>性别</td>
        <td>出生日期</td>
        <td>入职时间</td>
        <td>工资</td>
        <td>家庭地址</td>
        <td>部门</td>
        <td>操作</td>
    </tr>
    </thead>
    <tbody id="tbody">
    <tr>
        <td></td>
        <td></td>
        <td></td>
    </tr>
    </tbody>
    <tfoot id="tfoot">
    </tfoot>
</table>
<a href="/pages/emp_toadd.html">添加</a>
</body>
<script>
    var curPageindex = 1;
    var pagesize = 5;
    var pagecount = 1;
    $(document).ready(function () {
        getEmps(curPageindex);
    });

    function getEmps(pageindex) {
        $.ajax({
            type: "get",
            url: "/admin/empl/list/"+pageindex+"/"+pagesize,
            dataType: "json",
            success: function (result) {
                pagecount = result.data.pages;
                curPageindex = result.data.pageNum;
                $("#tbody").html("");
                for (var i = 0; i < result.data.list.length; i++) {
                    var emp = result.data.list[i];
                    var gender2;
                    if (emp.gender == 1) {
                        gender2 = "男"
                    } else {
                        gender2 = "女"
                    }
                    $("#tbody").append(`<tr>
        <td>${emp.empName}</td>
        <td>${gender2}</td>
        <td>${emp.birthday}</td>
        <td>${emp.hireDate}</td>
        <td>${emp.salary}</td>
        <td>${emp.address}</td>
        <td>${emp.dept.deptName}</td>
        <td>
            <a href="/pages/emp_toedit.html?id=${emp.id}">修改</a>
            <a href="javascript:empdel(${emp.id})" onclick="true confirm('是否删除？')">删除</a>
        </td>
    </tr>`);

                }
                initPager();
            }
        });
    }

    $(document).ready(function () {
        getEmps(curPageindex);
    });

    function empdel(empid) {
        $.ajax({
            type: "get",
            url: "/admin/empl/dodel/" + empid,
            dataType: "json",
            success: function (i) {
                if (i.errorCode == 200) {
                    getEmps(curPageindex);
                } else {
                    alert(i.errorMsg);
                }
            }
        });
    }
    function initPager(){
        $("#tfoot").html("");
        if(curPageindex<=1){

            $("#tfoot").append("首页 上一页");
        }else{
            $("#tfoot").append(`<a href="javascript:getEmps(1)">首页</a>
             <a href="javascript:getEmps(${curPageindex-1})">上一页</a>`);
        }
        if(curPageindex>=pagecount){

            $("#tfoot").append("下一页 末页");
        }else{
            $("#tfoot").append(`<a href="javascript:getEmps(${curPageindex+1})">下一页</a>
             <a href="javascript:getEmps(${pagecount})">末页</a>`);
        }
    }
</script>
</html>